package com.etasist;

/*
 * @auther: issa rabbba
 * @date: 2007-04-18
 * 
 */

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Vector;

import com.etasist.beans.*;

public class ClientConfigInfo {
	
	public static Vector servers = new Vector();
	public static HashMap hmap = new HashMap();

	public ClientConfigInfo(String fileName) {
		Properties properties = new Properties();
	    try {
	        properties.load(new FileInputStream(fileName));

	        getServers(properties);
	        
	    } catch (IOException e) {
	    	System.out.println("ERROR: in read the properties file.");
	    	e.printStackTrace();
	    }
	    
	}
/**
 * 
 * @parm @param prop
 */
	static void getServers(Properties prop){
		try{
			Enumeration en = prop.propertyNames();
			while (en.hasMoreElements()){
				String s = en.nextElement().toString();
				Servers clientServer = new Servers();
				if (s.indexOf("serverIp_")>-1){
					String[] ss = s.split("_");
					String serverID = ss[1];
					String[] serverIp_Port = prop.get(s).toString().split(":");
					clientServer.setServerIp(serverIp_Port[0]);
					clientServer.setServerPort(serverIp_Port[1]);
					System.out.print("Server "+serverID+" <"+serverIp_Port[0]+":"+serverIp_Port[1]+">");
					if(serverIp_Port.length>2){
						clientServer.setUserName(serverIp_Port[2]);
						clientServer.setPassword(serverIp_Port[3]);
						System.out.print("<Username="+serverIp_Port[2]+", password="+serverIp_Port[3]+">");
					}
					System.out.println();
					clientServer.setServerID(Integer.parseInt(serverID));
					clientServer.setAuthtype(getServerAuthType(prop, ""+serverID));
					clientServer.setServerCommands(getServerCommands(prop,""+serverID));
					servers.add(clientServer);
					hmap.put(serverID, clientServer);
				}
			}
				
		}catch (Exception e) {
			System.out.println("ERROR: in read servers list.");
			e.printStackTrace();
		}
	}
/**
 * 
 * @parm @param prop
 * @parm @param serverID
 * @parm @return ArrayList
 */
	static ArrayList getServerCommands(Properties prop, String serverID){
		ArrayList arl = new ArrayList();
		try{
			Enumeration en = prop.propertyNames();
			while (en.hasMoreElements()){
				String s = en.nextElement().toString();
				Commands commands = new Commands();
				if (s.indexOf("command.run.server_"+serverID+"_")>-1){
					String commandID = s.split("_")[2];
					System.out.println("Server "+serverID+" command "+commandID+" <"+prop.get(s)+">");
					commands.setCommand(prop.get(s).toString());
					commands.setCommandID(Integer.parseInt(commandID));
					arl.add(commands);
				}
			}
				
		}catch (Exception e) {
			System.out.println("ERROR: in read servers list.");
			e.printStackTrace();
		}
		return arl;
	}
	
	static String getServerAuthType(Properties prop, String serverID){
		try{
			Enumeration en = prop.propertyNames();
			while (en.hasMoreElements()){
				String s = en.nextElement().toString();
				if (s.indexOf("serverAuthType_"+serverID)>-1){
					return prop.get(s).toString();
				}
			}
				
		}catch (Exception e) {
			System.out.println("ERROR: in read servers list.");
			e.printStackTrace();
		}
		return "";
	}
}
